" gvimfullscreen.vim - set gvim  fullscreen for windows
"
" Maintainer: brightcxl (at) gmail.com
" Homepage: https://github.com/archerc/
"
" Last Modified: 2019/04/28 11:36
"
"     :ToggleFullscreen
"
" Settings:
"
" Variables:
if has('win32')
  let g:gvimfullscreen_lib_path_x86 = substitute(expand('<sfile>:p:h:h') . '/gvimfullscreen.x86.dll', '\', '/', 'g')
  let g:gvimfullscreen_lib_path_x64 = substitute(expand('<sfile>:p:h:h') . '/gvimfullscreen.x64.dll', '\', '/', 'g')
  if has('win64')
    let g:gvimfullscreen_lib_path = g:gvimfullscreen_lib_path_x64
  else
    let g:gvimfullscreen_lib_path = g:gvimfullscreen_lib_path_x86
  endif
endif

if filereadable(g:gvimfullscreen_lib_path)
endif

" Requirements:
"
" Examples:
"
" Additional:
"


function! gvimfullscreen#dll() abort
  if has_key(g:, 'gvimfullscreen_lib_path') && filereadable(g:gvimfullscreen_lib_path)
    return g:gvimfullscreen_lib_path
  endif
endfunction

function! gvimfullscreen#IsFullScreen(...) abort
  let dll = gvimfullscreen#dll()
  if filereadable(dll)
    return libcallnr(dll, "IsFullScreen", 0)
  endif
endfunction

function! gvimfullscreen#FullScreen() abort
  if !gvimfullscreen#IsFullScreen()
    call gvimfullscreen#ToggleFullScreen()
  endif
endfunction

function! gvimfullscreen#ToggleFullScreen(...)
  let dll = gvimfullscreen#dll()
  if filereadable(dll)
    if a:0 < 1
      let state = 0
    else
      let state = a:1
    endif
    call libcallnr(dll, "ToggleFullScreen", state)
  endif
endfunction

" SetAlpha
function! gvimfullscreen#SetAlpha(alpha)
  let dll = get(g:, 'gvimfullscreen_dll', gvimfullscreen#dll())
  if filereadable(dll)
    call libcall(dll, 'SetAlpha', a:alpha)
  endif
endfunction

" EnableTopMost
function! gvimfullscreen#EnableTopMost(enabled)
  let dll = get(g:, 'gvimfullscreen_dll', gvimfullscreen#dll())
  if filereadable(dll)
    call libcall(dll, 'EnableTopMost', a:enabled)
  endif
endfunction

function! gvimfullscreen#AddAlpha(alpha)
  if !has_key(g:, 'VimAlpha')
    let g:VimAlpha = 240
  endif
  let g:VimAlpha = g:VimAlpha + a:alpha
  if g:VimAlpha < 180
    let g:VimAlpha = 180
  endif
  if g:VimAlpha > 255
    let g:VimAlpha = 255
  endif
  call gvimfullscreen#SetAlpha(g:VimAlpha)
endfunction


""" TopMost
function! gvimfullscreen#SwitchVimTopMostMode()
  if !has_key(g:, 'VimTopMost')
    let g:VimTopMost = 0
  endif
  if g:VimTopMost == 0
    let g:VimTopMost = 1
  else
    let g:VimTopMost = 0
  endif
  call gvimfullscreen#EnableTopMost(g:VimTopMost)
endfunction

function! gvimfullscreen#define_mappings()
  nnoremap <Plug>(toggle-fullscreen) :<C-u>call gvimfullscreen#ToggleFullScreen()<CR>
  nnoremap <Plug>(vim-increase-alpha) <Esc>:call gvimfullscreen#AddAlpha(3)<CR>
  nnoremap <Plug>(vim-decrease-alpha) <Esc>:call gvimfullscreen#AddAlpha(-3)<CR>
  nnoremap <Plug>(toogle-top-most) <Esc>:call gvimfullscreen#SwitchVimTopMostMode()<CR>
  if !hasmapto('<Plug>(vim-increase-alpha)')
    "Shift+Y
    nmap <s-y> <Plug>(vim-increase-alpha)
  endif
  if !hasmapto('<Plug>(vim-decrease-alpha)')
    "Shift+T
    nmap <s-t> <Plug>(vim-decrease-alpha)
  endif
  if !hasmapto('<Plug>(toggle-top-most)')
    "Shift+R
    nmap <s-r> <Plug>(toggle-top-most)
  endif
  if !hasmapto('<Plug>(toggle-fullscreen)')
    "F11
    nmap <F11> <Plug>(toggle-fullscreen)
  endif
endfunction

function! gvimfullscreen#define_commands()
  command! -nargs=0 ToggleFullScreen    :call gvimfullscreen#ToggleFullScreen()
  command! -nargs=0 FullScreen          :call gvimfullscreen#FullScreen()
  command! -nargs=0 MaximizeWindow      :simalt ~X<CR>
endfunction

function! gvimfullscreen#init()
  let g:gvimfullscreen_dll = gvimfullscreen#dll()
  call gvimfullscreen#define_mappings()
  call gvimfullscreen#define_commands()
endfunction

" vim: set et fenc=utf-8 ff=unix sts=4 sw=2 ts=2 :
